package cn.fuxue.hrms.web.admin_controller;

import cn.fuxue.hrms.common.entity.User;
import cn.fuxue.hrms.service.UserService;
import cn.fuxue.hrms.service.impl.UserServiceImpl;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller()
@RequestMapping(value = "/admin/user",produces = "text/plain;charset=utf-8")
public class UserController {

    @Autowired
    private UserServiceImpl userService;


    @GetMapping("/find")
    public String find(Model model) {
        List<User> userList=userService.findAll();
        model.addAttribute("userList", userList);
        return "admin/permissions_user";
    }

    @GetMapping("/find/{id}")
    public String findById(@PathVariable Integer id, Model model) {
        User user=userService.selectByPrimaryKey(id);
        model.addAttribute("user", user);
        return "admin/userDetail";
    }

    @GetMapping("/delete/{id}")
    public String delete(@PathVariable Integer id) {
        userService.deleteByPrimaryKey(id);
        return "redirect:/admin/user/find";
    }

    @GetMapping("/find/id")
    @ResponseBody
    public String findId(Model model) {
        return JSON.toJSONString(userService.findId());
    }

    @GetMapping("/find/candidate")
    @ResponseBody
    public String findCandidates(Model model) {
        return JSON.toJSONString(userService.findCandidates());
    }



}
